<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="java.text.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.ParsePosition" %>

<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%@ page import="schoolsource.util.Types" %>
<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
    String requestObject = (String) st.getValue(FormFields.TOKEN);
    int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
    ClassContainer classC[] = (ClassContainer[]) st.getValue("classContainer");
    UserContainer studentC[] = (UserContainer[]) st.getValue("studentsInClass");
//    ClassAttendance classAttendance[];
    String loginType = (String) request.getAttribute(FormFields.USRTYPE);
    String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);

    String[] nameOfMonth = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    GregorianCalendar gc[] = new GregorianCalendar[25];
    GregorianCalendar date = new GregorianCalendar();
    date.set(Calendar.DAY_OF_MONTH, 1);
    String dateString = null;
    String displayMessage = (String) st.getValue("displayMessage");
    st.putValue("displayMessage", "");

    if ((dateString = (String) st.getValue(FormFields.ATTENDANCEDATE)) != null && !"".equals(dateString)) {
        date.setTime(Types.sdfMMDDYYYY.parse(dateString));
    }
    date.set(Calendar.DAY_OF_MONTH, 1);

    int dayNum;
    if (date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        date.add(Calendar.DAY_OF_YEAR,1);
        dayNum = 0;
    }
    else if (date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
        date.add(Calendar.DAY_OF_YEAR,2);
        dayNum = 0;
    }
    else {
        dayNum = date.get(Calendar.DAY_OF_WEEK) - 2;
    }

    int dayOfMonth = date.get(Calendar.DAY_OF_MONTH);
    for (int day = dayNum; day < gc.length; ++day) {
        gc[day] = new GregorianCalendar(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
        gc[day].set(Calendar.DAY_OF_MONTH, dayOfMonth);

        if(gc[day].get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            dayOfMonth = dayOfMonth+2;
            gc[day].set(Calendar.DAY_OF_MONTH, dayOfMonth);
        }
        else if (gc[day].get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            dayOfMonth = dayOfMonth+1;
            gc[day].set(Calendar.DAY_OF_MONTH, dayOfMonth);
        }
        dayOfMonth++;
    }

%>



<HTML>
  <HEAD>
      <TITLE>Class Attendance</TITLE>

      <SCRIPT language="JavaScript">

          function goPrevMonth() {

              document.forms.classattendance.<%= FormFields.ATTENDANCEDATE %>.value = "<% GregorianCalendar prev = new GregorianCalendar(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
                                                                                         prev.add(Calendar.MONTH, -1);
                                                                                         out.print(Types.sdfMMDDYYYY.format(prev.getTime()));
                                                                                      %>";
              document.forms.classattendance.<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.ATTENDANCESUMMARY %>";
              document.forms.classattendance.submit();
          }

          function goNextMonth() {
              document.forms.classattendance.<%= FormFields.ATTENDANCEDATE %>.value = "<% GregorianCalendar next = new GregorianCalendar(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
                                                                                         next.add(Calendar.MONTH, 1);
                                                                                         out.print(Types.sdfMMDDYYYY.format(next.getTime()));
                                                                                      %>";
              document.forms.classattendance.<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.ATTENDANCESUMMARY %>";
              document.forms.classattendance.submit();
          }

          function takeAttendance(targetDate) {
              document.forms.classattendance.<%= FormFields.ATTENDANCEDATE %>.value = targetDate;
              document.forms.classattendance.<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.ATTENDANCEEDIT %>";
              document.forms.classattendance.submit();
              return false;
          }
      </SCRIPT>
  </HEAD>

  <BODY <%= (displayMessage != null && !"".equals(displayMessage)) ? "onLoad=\"top.frames.mainframe.notify('" + displayMessage + "')\"" : "" %>>
      <FORM NAME="classattendance" METHOD="POST">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.ATTENDANCEDATE %>" VALUE="">
<CENTER><H2>Attendance Calendar<BR>
        <% 
        out.println(""+nameOfMonth[date.get(Calendar.MONTH)]+" "+date.get(Calendar.YEAR)+"</H2></CENTER>");
        %>
    <TABLE ALIGN="CENTER" BORDER="0" WIDTH="400">
      <TR>
        <TD ALIGN="LEFT">
          <INPUT TYPE="BUTTON" NAME="PreviousMonth" VALUE="Previous Month" onClick="goPrevMonth();">
        </TD>
	<TD ALIGN="CENTER">
	</TD>
        <TD ALIGN="RIGHT">
          <INPUT TYPE="BUTTON" NAME="NextMonth" VALUE="Next Month" onClick="goNextMonth();">
        </TD>
      </TR>
    </TABLE>

    <TABLE ALIGN="CENTER" WIDTH="400">
        <TR>
            <TD ALIGN="LEFT">
    	        <H5>* Click on a date to take attendance</H5>
            </TD>
        </TR>
    </TABLE>
    <TABLE ALIGN="CENTER" border="1" cellspacing="0">
        
            <TR bgcolor="WHITE">
                <TH width=80>Mon</TH>
                <TH width=80>Tue</TH>
                <TH width=80>Wed</TH>
                <TH width=80>Thu</TH>
                <TH width=80>Fri</TH>
            </TR>

      <%
        out.println("<TR bgcolor=\"TAN\">");
        for (int w = 0; w < gc.length; w++){
            if ((w % 5) == 0) {
                if (w == gc.length - 1) {
                    out.println("</TR>");
                }
                else {
                    out.println("</TR><TR bgcolor=\"TAN\">");
                }
            }
            if (gc[w] != null) {
                out.println("<TD width=\"80\" height=\"80\" align=\"left\" valign=\"TOP\"><a href=\"\" onClick=\"takeAttendance('" + Types.sdfMMDDYYYY.format(gc[w].getTime()) + "');return false;\"><FONT COLOR=\"black\" size=\"2\">" + Types.sdfMMDD.format(gc[w].getTime())+"</FONT></a><br>\n");
                out.println("<hr/>");
                out.println("<table>");
                out.println("<tr><td>");
                ClassAttendance ca = ClassAttendance.getClassAttendance(classC[classnum].getClassAssignedID().toString(), Types.sdfMMDDYYYYslash.format(gc[w].getTime()), (String) st.getValue(FormFields.SCHOOLNAME));
                out.println("</td><td>Tardy: </td><td>" + ca.getTotalTardies() + "</td></tr>");
                out.println("<tr><td>");
                out.println("</td><td>Absent: </td><td>" + ca.getTotalAbsences() + "</td></tr>");
                out.println("</table>");
            }
            else {
                out.println("<TD width=\"80\" height=\"80\" align=\"left\" valign=\"TOP\"><FONT COLOR=\"black\" size=\"2\">&nbsp</FONT><br>\n");
            }
            out.println("</TD>");
        }
      %>
    </TABLE>
   </FORM>
  </BODY>
</HTML>